
const {ccclass, property} = cc._decorator;

@ccclass
export default class Player extends cc.Component {

    // @property(cc.Label)
    // label: cc.Label = null;

    // @property
    // text: string = 'hello';

    // 主角跳跃高度
    @property
    jumpHeight:number =  0;
    // 主角跳跃持续时间
    @property
    jumpDuration:number = 0;
    // 最大移动速度
    @property
    maxMoveSpeed:number = 0;
    // 加速度
    @property
    accel:number = 0;
    // 加速度方向开关
    @property
    accLeft:boolean = false;
    @property
    accRight:boolean = false;
    // 主角当前水平方向速度
    @property
    xSpeed:number = 0;
    // 跳跃音效
    @property(cc.AudioClip)
    jumpAudio:cc.AudioClip = null;

    // LIFE-CYCLE CALLBACKS:

    private maxX:number;
    private minX:number;

    onLoad () {
        let jumpAction = this.runJumpAction();
        cc.tween(this.node).then(jumpAction).start()

        // 初始化键盘输入监听
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this); 
        
        this.maxX = (cc.winSize.width/2 - this.node.width/2);
        this.minX = (-cc.winSize.width/2 + this.node.width/2);
    }

    start () {

    }
    // 跳跃方法
    runJumpAction() {
        // 跳跃上升
        let jumpUp = cc.tween().by(this.jumpDuration, {y: this.jumpHeight}, {easing: 'sineOut'});
        // 下落
        let jumpDown = cc.tween().by(this.jumpDuration, {y: -this.jumpHeight}, {easing: 'sineIn'});

        // 创建一个缓动，按 jumpUp、jumpDown 的顺序执行动作
        let tween = cc.tween().sequence(jumpUp, jumpDown)
                            // 添加一个回调函数，在前面的动作都结束时调用我们定义的 playJumpSound() 方法
                            .call(this.playJumpSound, this);

        // 不断重复
        return cc.tween().repeatForever(tween);
    }
    // 调用声音引擎播放声音
    playJumpSound() {
        // 调用声音引擎播放声音
        cc.audioEngine.playEffect(this.jumpAudio, false);
        cc.audioEngine.setEffectsVolume(0.2);
    }

    // 键盘按下事件
    onKeyDown (event: cc.Event.EventKeyboard) {
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft = true;
                break;
            case cc.macro.KEY.d:
                this.accRight = true;
                break;
        }
    }
    // 键盘抬起事件
    onKeyUp (event: cc.Event.EventKeyboard) {
        
        // unset a flag when key released
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft = false;
                break;
            case cc.macro.KEY.d:
                this.accRight = false;
                break;
        }
    }

    onDestroy () {
        // 取消键盘输入监听
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }
    // dt 是时间
    update (dt: number) {
        // 根据当前加速度方向每帧更新速度
        if (this.accLeft) {
            this.xSpeed = -this.accel;
        }
        else if (this.accRight) {
            this.xSpeed = this.accel;
        } else {
            this.xSpeed = 0;
        }

        // 限制主角的速度不能超过最大值a
        if (Math.abs(this.xSpeed) > this.maxMoveSpeed) {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 限制移动范围
        
        if (this.node.x >= this.maxX) {
            this.node.x = this.maxX;
        } else if (this.node.x <= this.minX) {
            this.node.x =  this.minX;
        }

        // 根据当前速度更新主角的位置
        this.node.x += this.xSpeed * dt;
    }
}
